package com.winchain.chainbill.bill.biz.discount.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.TxCmd;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctTranCmd extends TxCmd<CbDct> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private CusRecService cusRecService;

	public boolean execute(CommandContext<Bill<CbDct>> context) throws Exception {
		Object timeout = context.getValue("Timeout");
		if ((timeout == null) || ((timeout != null) && (!((Boolean) timeout).booleanValue()))) {
			CbRec record = ((Bill) context.getSource()).getRec();
			CbDct cbDct = (CbDct) ((Bill) context.getSource()).getSource();
			String billId = record.getBillId();

			List<Bill<CbDct>> children = (List) context.getValue("children");
			if ((children != null) && (children.size() > 0)) {
				for (Bill<CbDct> bill : children) {
					if (StringUtil.notEmpty(((CbDct) bill.getSource()).getDctDate())) {
						cbDct = (CbDct) bill.getSource();
						billId = bill.getRec().getBillId();
					}
				}
			}
			AcctTxRec tran = getAcctTxRec(context, cbDct, billId);
			this.tradeService.callback(tran);
		}
		return false;
	}

	private AcctTxRec getSerFeeAcctTxRec(AcctTxRec tran, CbDct cbDct) {
		AcctTxRec serFeeTxRec = new AcctTxRec();
		BeanUtils.copyProperties(tran, serFeeTxRec);

		CusRec platform = this.cusRecService.getPlatform(null);
		AcctBas acct = getAcctBas(platform.getCusId(), "1", null);
		serFeeTxRec.setCpAcctNo(acct.getAcctNo());
		serFeeTxRec.setCpAcctNm(acct.getAcctNm());
		serFeeTxRec.setCpBankNm(acct.getFiNm());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(acct.getAcctNo(), tran.getCurrCode(), null, null);
		serFeeTxRec.setCpBkpId(cpAcctBkp.getBkpId());

		serFeeTxRec.setTxId(SystemUtil.getUUID());
		serFeeTxRec.setTxSn(serFeeTxRec.getTxId());
		serFeeTxRec.setTxAmt(cbDct.getSerFee());
		serFeeTxRec.setTxType("cb_dct_fee");
		serFeeTxRec.setTxAbs("������������-������");
		serFeeTxRec.setTxDt(SysDateUtil.getSystemDate());
		serFeeTxRec.setTxTs(SysDateUtil.getSystemDateTime());
		return serFeeTxRec;
	}

	private AcctTxRec getAcctTxRec(CommandContext<Bill<CbDct>> context, CbDct cbDct, String billId) {
		String optType = "qt_trd";
		BigDecimal txAmt = cbDct.getActualAmt();

		Object bizType = context.getValue("bizType");
		if (bizType == null) {
			throw new AcctException("������������������");
		}
		String abs = "";
		String creditAcctUse = "";
		String acctUse = "";
		if ("cb_dct_rev".equals(bizType)) {
			abs = "������������";
		} else {
			abs = "������������";
		}
		creditAcctUse = "1";
		acctUse = "1";

		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setCurrCode(AssetCode.XCB.name());
		txRec.setTxChnCode("C");

		AcctBas tranAcct = getAcctBas(cbDct.getSellerId(), creditAcctUse, null);

		txRec.setTxAcctNo(tranAcct.getAcctNo());
		txRec.setTxAcctNm(tranAcct.getAcctNm());
		txRec.setTxBankNm(tranAcct.getFiNm());
		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());

		txRec.setTxOperator(CommonDataUtil.getUserName());
		txRec.setTxAmt(txAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType((String) bizType);
		txRec.setOptType(optType);

		txRec.setTxSts("I");

		AcctBas acct = getAcctBas(cbDct.getFinId(), acctUse, null);

		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");
		return txRec;
	}
}
